<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('pages/loginpage');
	}
	
	public function login()
	{
		$this->load->helper(array('url','form'));
		$this->load->library('form_validation');
		
		/* Get input of the user. */
		$uname = $this->input->post('uname');
		$password = $this->input->post('password');
		$role = $this->input->post('role');
		
		/* 1st param is the rule. 2nd param is the the text you want to be displayed. */
		$this->form_validation->set_message('integer', 'Role is required.'); 
		/* To modify the position of the error messages(?) Paexplore nalang vince :)) */
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
		/* Rules */
		$this->form_validation->set_rules('uname','username','trim|required');
		$this->form_validation->set_rules('password','password','trim|required|min_length[6]|max_length[20]');
		$this->form_validation->set_rules('role','role','trim|required|integer');
		
		if($this->form_validation->run() == FALSE){
			$this->load->view('pages/loginpage');
		}else{
			$this->load->model('user_model');
			$result = $this->user_model->check_login($uname,$password,$role);
			
			if($result == true){
				if($role == 1) $this->load->view('pages/admin_homepage');
				else if($role == 2) $this->load->view('pages/encoder_homepage');
				else if($role == 3) $this->load->view('pages/student_homepage');
				else if($role == 4) $this->load->view('pages/instructor_homepage');
			}else $this->load->view('pages/login_errorpage');
		}
	}
	
	public function signup(){
		$this->load->helper(array('url','form'));
		$this->load->library('form_validation');
		
		/* Get input of the user. */
		$lname = $this->input->post('lname');
		$fname = $this->input->post('fname');
		$minitial = $this->input->post('minitial');
		$stdno = $this->input->post('stdno');
		$username = $this->input->post('username');
		$pw1 = $this->input->post('pw1');
		$pw2 = $this->input->post('pw2');
	
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
		$this->form_validation->set_rules('lname','last name','trim|required');
		$this->form_validation->set_rules('fname','first name','trim|required');
		$this->form_validation->set_rules('minitial','middle initial','trim|required');
		$this->form_validation->set_rules('stdno','student number','trim|required|integer|min_length[9]|max_length[9]');
		$this->form_validation->set_rules('username','username','trim|required');
		$this->form_validation->set_rules('pw1', 'password', 'trim|required|min_length[6]|max_length[20]|matches[pw2]|md5');
		$this->form_validation->set_rules('pw2', 'password confirmation', 'trim|required|min_length[6]|max_length[20]');
		
		if($this->form_validation->run() == FALSE){
			$this->load->view('pages/loginpage');
		}else{
			$this->load->model('user_model');
			$name = $fname . " " . $minitial . " " . $lname;
			/* Approved and Activated set to zero first. */
			$r = $this->user_model->check_signup($stdno,$name,0,0,$username,$pw1);
			
			if($r == 1) $this->load->view('pages/signup_errorpage1');
			if($r == 2) $this->load->view('pages/signup_errorpage2');
			if($r == 3) $this->load->view('pages/signup_successful');
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */